package com.youaishujuhui.devops.web.controller.manage;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.pager.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.StringUtils;
import com.youaishujuhui.devops.web.domain.manage.SysAuditLog;
import com.youaishujuhui.devops.web.service.manage.SysAuditLogService;
import com.youaishujuhui.devops.web.vo.Page;
import com.youaishujuhui.devops.web.vo.Result;

@Controller
@RequestMapping("/manage/log")
public class SysLogController {
	
	@Autowired
	private SysAuditLogService sysAuditLogService;

    @RequestMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("manage/log");
    }
	
    @RequestMapping("/query")
    @ResponseBody
    public Result<List<SysAuditLog>> query(Integer limit,Integer offset,String sort,String order){
        Result<List<SysAuditLog>> result = new Result<>();
        int total = sysAuditLogService.count();
        
		Cnd cnd = Cnd.NEW();
		
		if (false == StringUtils.isNullOrEmpty(sort)) {
			if (order.equals("asc")) {
				cnd.asc(sort);
			} else {
				cnd.desc(sort);
			}
		} else {
			cnd.asc(sort);
		}
		
		Pager pager = new Pager(offset / limit + 1, limit);

        List<SysAuditLog> list = sysAuditLogService.query(cnd, pager);
        Page page = new Page(total,limit);
        result.setPage(page);
        result.setData(list);
        result.setCode(Result.SUCCESS);

        return result;
    }
}
